/**
 * 
 */
package com.nari.basic.action;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.IBuilding;
import com.nari.baseinfo.model.ICommunity;
import com.nari.baseinfo.model.IHeatRoom;
import com.nari.baseinfo.model.IProject;
import com.nari.baseinfo.service.IBuildingService;
import com.nari.baseinfo.service.ICommunityService;
import com.nari.baseinfo.service.IHeatRoomService;
import com.nari.baseinfo.service.IProjectService;
import com.nari.basic.model.IUser;
import com.nari.basic.service.IBasicResourceCascadeMgrService;
import com.nari.basic.service.IUserRoleService;
import com.nari.basic.service.IUserService;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.coreframe.webframe.SysEnvironmentMgr;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.platform.model.WebConstants;
import com.nari.platform.security.MD5Encoder;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2013-7-18
 */
public class UserAction extends JSONActionSupport {
	private static final long serialVersionUID = 8417882730023904727L;
	private IUserService iUserService;
	private IUserRoleService iUserRoleService;
	private IBasicResourceCascadeMgrService iBasicResourceCascadeMgrService;
	//用户在项目中所属关系设置
	private IProjectService iProjectService;
	private ICommunityService iCommunityService;
	private IBuildingService iBuildingService;
	private IHeatRoomService iHeatRoomService;
	private List<IProject> projectList;
	private List<ICommunity> communityList;
	private List<IBuilding> buildingList;
	private List<IHeatRoom> roomList;
	// 查询用户列表
	private int pageIndex = 1; // 默认查询第一页用户列表
	private PageCond pageCond;
	private int pageSize;
	private String userID;
	private String pid;
	private ResultWithPageCond<IUser> userPage;
	// 用户信息管理
	private String userUID;
	private IUser user;
	private String rePassword;
	// 角色用户授权
	private String roleUID;
	private List<IUser> userList;
	private String userUIDList;
	private String projectID;
	private String communityID;
	private String buildingId;
	private String roomId;
	//修改密码
	private String sysSecurity;
	private String currPassword;
	private String setupPassword;
	private String reSetupPassword;

	/**
	 * 分页查询用户列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-24
	 */
	public String queryUserPage() throws Exception {
		// 分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		// 根据分页信息查询用户列表
		this.userPage = this.iUserService.queryUserWithResultPage(userID, pid, pageCond);
		// 计算结果集的页数
		this.pageSize = new BigDecimal(this.userPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.userPage.getPageCond().getLimit()), RoundingMode.UP)
				.intValue();
		if (pageSize == 0) pageSize = 1;
		return "userList";
	}

	/**
	 * 用户信息管理
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-25
	 */
	public String queryUserForManage() throws Exception {
		if (this.userUID != null && !"".equals(this.userUID.trim())) {
			this.user = this.iUserService.queryEntityById(userUID);
		}
		//查询用户所属信息
		if(user!=null && user.getRoomId()!=null) {
			this.roomId = user.getRoomId();
			IHeatRoom heatRoom = iHeatRoomService.queryEntityById(roomId);
			if(heatRoom != null) {
				this.projectID = heatRoom.getProjectID();
				this.communityID = heatRoom.getCommunityID();
				this.buildingId = heatRoom.getBuildingID();
			}
		}
		// 所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if (this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		// 所属小区列表
		if (projectID != null && !"".equals(projectID)) {
			this.communityList = this.iCommunityService.queryCommunitysByBelongings(null, null, null, projectID);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		// 所属楼栋列表
		this.buildingList = this.iBuildingService.queryBuildingByBelongings(projectID, communityID);
		// 所属楼栋列表、所属分摊区域列表
		if ((this.projectID != null && !"".equals(projectID))
				|| (this.communityID != null && !"".equals(communityID))
				|| (buildingId != null && !"".equals(buildingId))) {
			this.roomList = this.iHeatRoomService.queryHeatRoomsByBelongs(projectID, communityID, null, buildingId);
		} else {
			this.roomList = new ArrayList<IHeatRoom>();
		}
		return "userManage";
	}

	/**
	 * 新增或修改用户信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-25
	 */
	public String saveOrUpdateUser() throws Exception {
		if (!this.user.getPassword().equals(rePassword)) {
			this.rmsg = ResponseMsg.getInstance(false, "两次输入的密码不一致！");
			this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
			return Action.SUCCESS;
		}
		String password = MD5Encoder.getMD5String(this.user.getPassword());
		this.user.setPassword(password);
		if (this.user.getId() != null && !"".equals(this.user.getId().trim())) {
			this.iUserService.updateEntity(user, this.user.getId());
		} else {
			this.iUserService.saveEntity(user);
		}
		this.writeJsonStrToResponse(JSONUtil.serialize(ResponseMsg.getInstance(true, "")));
		return Action.SUCCESS;
	}

	/**
	 * 删除用户信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-25
	 */
	public String deleteUserByUserUID() throws Exception {
		this.rmsg = iBasicResourceCascadeMgrService.deleteUserCascadeByUserUID(userUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}

	/**
	 * 通过角色唯一标识查询角色用户列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-26
	 */
	public String queryUserByRoleUID() throws Exception {
		// 分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		// 根据分页信息查询用户列表
		this.userPage = this.iUserService.queryUserByRoleUID(roleUID, userID, pid, pageCond);
		// 计算结果集的页数
		this.pageSize = new BigDecimal(this.userPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.userPage.getPageCond().getLimit()), RoundingMode.UP)
				.intValue();
		if (pageSize == 0) pageSize = 1;
		return "roleUserList";
	}

	/**
	 * 查询不带指定角色的用户列表
	 * 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-26
	 */
	public String queryUserWithoutRole() throws Exception {
		// 分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		// 根据分页信息查询用户列表
		this.userPage = this.iUserService.queryUserPageWithoutRole(roleUID, userID, pid, pageCond);
		// 计算结果集的页数
		this.pageSize = new BigDecimal(this.userPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.userPage.getPageCond().getLimit()), RoundingMode.UP)
				.intValue();
		if (pageSize == 0) pageSize = 1;
		return "userPageWithoutRole";
	}

	/**
	 * 授权用户角色
	 * 
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-29
	 */
	public String authorizeUserRole() throws Exception {
		this.iUserRoleService.saveUserRoleAuthority(roleUID, userUIDList);
		this.writeJsonStrToResponse(JSONUtil.serialize(ResponseMsg.getInstance(true, "")));
		return Action.SUCCESS;
	}

	/**
	 * 取消用户角色授权
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-29
	 */
	public String cancelUserRole() throws Exception {
		this.iUserRoleService.deleteUserRoleAuthority(roleUID, userUIDList);
		this.writeJsonStrToResponse(JSONUtil.serialize(ResponseMsg.getInstance(true, "")));
		return Action.SUCCESS;
	}

	/**
	 * 查看个人信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-7-30
	 */
	public String showPersonalInfo() throws Exception {
		HttpSession session = SysEnvironmentMgr.getSessionInstance();
		this.user = (IUser) session.getAttribute(WebConstants.USER_SESSION);
		return "personalInfo";
	}
	
	/**
	 * 加载密码修改页面
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-8
	 */
	public String loadPwdModifyPage() throws Exception {
		//携带会话参数
		return "pwdModifyPage";
	}
	
	/**
	 * 检查用户密码是否正确
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-9
	 */
	public String verifyUserPwd() throws Exception {
		HttpSession session = SysEnvironmentMgr.getSessionInstance();
		this.user = (IUser) session.getAttribute(WebConstants.USER_SESSION);
		String password = MD5Encoder.getMD5String(this.currPassword);
		//初始化返回信息
		this.rmsg = ResponseMsg.getInstance(true, "");
		if(!password.equals(user.getPassword())) {
			this.rmsg = ResponseMsg.getInstance(false, "原密码输入错误！");
		}
		this.writeJsonStrToResponse(JSONUtil.serialize(this.rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 修改用户密码
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-9
	 */
	public String modifyUserPwd() throws Exception {
		HttpSession session = SysEnvironmentMgr.getSessionInstance();
		this.user = (IUser) session.getAttribute(WebConstants.USER_SESSION);
		this.rmsg = ResponseMsg.getInstance(true, ""); //初始化返回信息
		//检测是否系统管理员
		String userID = sysSecurity.substring(0, sysSecurity.indexOf("="));
		if(userID.equals(user.getUserID())) {
			this.rmsg = ResponseMsg.getInstance(false, "<"+userID+">密码不可以修改！");
		} else {
			String password = MD5Encoder.getMD5String(this.currPassword);
			if(!password.equals(user.getPassword())) { //校验原密码
				this.rmsg = ResponseMsg.getInstance(false, "原密码输入错误！");
			} else { //原密码输入正确，校验新密码
				if(!setupPassword.equals(reSetupPassword)) {
					this.rmsg = ResponseMsg.getInstance(false, "新密码与重复新密码不一致，请重新输入！");
				} else { //更正用户密码
					password = MD5Encoder.getMD5String(this.setupPassword);
					user.setPassword(password);
					iUserService.updateEntity(user, user.getId());
				}
			}
		}
		this.writeJsonStrToResponse(JSONUtil.serialize(this.rmsg));
		return Action.SUCCESS;
	}

	/**
	 * @return the iUserService
	 */
	@JSON(serialize = false)
	public IUserService getiUserService() {
		return iUserService;
	}

	/**
	 * @param iUserService the iUserService to set
	 */
	public void setiUserService(IUserService iUserService) {
		this.iUserService = iUserService;
	}
	
	/**
	 * @return the iUserRoleService
	 */
	@JSON(serialize = false)
	public IUserRoleService getiUserRoleService() {
		return iUserRoleService;
	}

	/**
	 * @param iUserRoleService the iUserRoleService to set
	 */
	public void setiUserRoleService(IUserRoleService iUserRoleService) {
		this.iUserRoleService = iUserRoleService;
	}
	
	/**
	 * @return the iBasicResourceCascadeMgrService
	 */
	@JSON(serialize = false)
	public IBasicResourceCascadeMgrService getiBasicResourceCascadeMgrService() {
		return iBasicResourceCascadeMgrService;
	}

	/**
	 * @param iBasicResourceCascadeMgrService the iBasicResourceCascadeMgrService to set
	 */
	public void setiBasicResourceCascadeMgrService(
			IBasicResourceCascadeMgrService iBasicResourceCascadeMgrService) {
		this.iBasicResourceCascadeMgrService = iBasicResourceCascadeMgrService;
	}

	/**
	 * @return the iProjectService
	 */
	@JSON(serialize = false)
	public IProjectService getiProjectService() {
		return iProjectService;
	}

	/**
	 * @param iProjectService the iProjectService to set
	 */
	public void setiProjectService(IProjectService iProjectService) {
		this.iProjectService = iProjectService;
	}

	/**
	 * @return the iCommunityService
	 */
	@JSON(serialize = false)
	public ICommunityService getiCommunityService() {
		return iCommunityService;
	}

	/**
	 * @param iCommunityService the iCommunityService to set
	 */
	public void setiCommunityService(ICommunityService iCommunityService) {
		this.iCommunityService = iCommunityService;
	}

	/**
	 * @return the iBuildingService
	 */
	@JSON(serialize = false)
	public IBuildingService getiBuildingService() {
		return iBuildingService;
	}

	/**
	 * @param iBuildingService the iBuildingService to set
	 */
	public void setiBuildingService(IBuildingService iBuildingService) {
		this.iBuildingService = iBuildingService;
	}

	/**
	 * @return the iHeatRoomService
	 */
	@JSON(serialize = false)
	public IHeatRoomService getiHeatRoomService() {
		return iHeatRoomService;
	}

	/**
	 * @param iHeatRoomService the iHeatRoomService to set
	 */
	public void setiHeatRoomService(IHeatRoomService iHeatRoomService) {
		this.iHeatRoomService = iHeatRoomService;
	}

	/**
	 * @return the projectList
	 */
	public List<IProject> getProjectList() {
		return projectList;
	}

	/**
	 * @param projectList the projectList to set
	 */
	public void setProjectList(List<IProject> projectList) {
		this.projectList = projectList;
	}

	/**
	 * @return the communityList
	 */
	public List<ICommunity> getCommunityList() {
		return communityList;
	}

	/**
	 * @param communityList the communityList to set
	 */
	public void setCommunityList(List<ICommunity> communityList) {
		this.communityList = communityList;
	}

	/**
	 * @return the buildingList
	 */
	public List<IBuilding> getBuildingList() {
		return buildingList;
	}

	/**
	 * @param buildingList the buildingList to set
	 */
	public void setBuildingList(List<IBuilding> buildingList) {
		this.buildingList = buildingList;
	}

	/**
	 * @return the roomList
	 */
	public List<IHeatRoom> getRoomList() {
		return roomList;
	}

	/**
	 * @param roomList the roomList to set
	 */
	public void setRoomList(List<IHeatRoom> roomList) {
		this.roomList = roomList;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the userID
	 */
	public String getUserID() {
		return userID;
	}

	/**
	 * @param userID the userID to set
	 */
	public void setUserID(String userID) {
		this.userID = userID;
	}

	/**
	 * @return the pid
	 */
	public String getPid() {
		return pid;
	}

	/**
	 * @param pid the pid to set
	 */
	public void setPid(String pid) {
		this.pid = pid;
	}

	/**
	 * @return the userPage
	 */
	public ResultWithPageCond<IUser> getUserPage() {
		return userPage;
	}

	/**
	 * @param userPage the userPage to set
	 */
	public void setUserPage(ResultWithPageCond<IUser> userPage) {
		this.userPage = userPage;
	}

	/**
	 * @return the userUID
	 */
	public String getUserUID() {
		return userUID;
	}

	/**
	 * @param userUID the userUID to set
	 */
	public void setUserUID(String userUID) {
		this.userUID = userUID;
	}

	/**
	 * @return the user
	 */
	public IUser getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(IUser user) {
		this.user = user;
	}

	/**
	 * @return the rePassword
	 */
	public String getRePassword() {
		return rePassword;
	}

	/**
	 * @param rePassword the rePassword to set
	 */
	public void setRePassword(String rePassword) {
		this.rePassword = rePassword;
	}

	/**
	 * @return the roleUID
	 */
	public String getRoleUID() {
		return roleUID;
	}

	/**
	 * @param roleUID the roleUID to set
	 */
	public void setRoleUID(String roleUID) {
		this.roleUID = roleUID;
	}

	/**
	 * @return the userList
	 */
	public List<IUser> getUserList() {
		return userList;
	}

	/**
	 * @param userList the userList to set
	 */
	public void setUserList(List<IUser> userList) {
		this.userList = userList;
	}

	/**
	 * @return the userUIDList
	 */
	public String getUserUIDList() {
		return userUIDList;
	}

	/**
	 * @param userUIDList the userUIDList to set
	 */
	public void setUserUIDList(String userUIDList) {
		this.userUIDList = userUIDList;
	}

	public String getProjectID() {
		return projectID;
	}

	public void setProjectID(String projectID) {
		this.projectID = projectID;
	}

	public String getCommunityID() {
		return communityID;
	}

	public void setCommunityID(String communityID) {
		this.communityID = communityID;
	}

	public String getBuildingId() {
		return buildingId;
	}

	public void setBuildingId(String buildingId) {
		this.buildingId = buildingId;
	}

	/**
	 * @return the roomId
	 */
	public String getRoomId() {
		return roomId;
	}

	/**
	 * @param roomId the roomId to set
	 */
	public void setRoomId(String roomId) {
		this.roomId = roomId;
	}

	/**
	 * @return the sysSecurity
	 */
	public String getSysSecurity() {
		return sysSecurity;
	}

	/**
	 * @param sysSecurity the sysSecurity to set
	 */
	public void setSysSecurity(String sysSecurity) {
		this.sysSecurity = sysSecurity;
	}

	/**
	 * @return the currPassword
	 */
	public String getCurrPassword() {
		return currPassword;
	}

	/**
	 * @param currPassword the currPassword to set
	 */
	public void setCurrPassword(String currPassword) {
		this.currPassword = currPassword;
	}

	/**
	 * @return the setupPassword
	 */
	public String getSetupPassword() {
		return setupPassword;
	}

	/**
	 * @param setupPassword the setupPassword to set
	 */
	public void setSetupPassword(String setupPassword) {
		this.setupPassword = setupPassword;
	}

	/**
	 * @return the reSetupPassword
	 */
	public String getReSetupPassword() {
		return reSetupPassword;
	}

	/**
	 * @param reSetupPassword the reSetupPassword to set
	 */
	public void setReSetupPassword(String reSetupPassword) {
		this.reSetupPassword = reSetupPassword;
	}

}
